// BEGIN CUT HERE
/*
PROBLEM STATEMENT

You are given two positive integers x and k. Return the k-th smallest positive integer y (where k is 1-based) for 
which the following equation holds:
x + y = x | y
where '|' denotes the bitwise OR operator.



DEFINITION
Class:BitwiseEquations
Method:kthPlusOrSolution
Parameters:int, int
Returns:long long
Method signature:long long kthPlusOrSolution(int x, int k)


CONSTRAINTS
-x will be between 1 and 2,000,000,000, inclusive.
-k will be between 1 and 2,000,000,000, inclusive.


EXAMPLES

0)
5
1

Returns: 2

The first positive integer for which the equation holds is 2.
You can check that 5+2=7 as well as 5|2=7. Both plus and bitwise OR look like the following:

 101
+ 10
 ---
 111



1)
5
5

Returns: 18

The fifth number for which the equation 5 + y = 5 | y holds is 18. The first four solutions are 2,8,10,16.
The binary sum for 18 looks like the following:

   101
+10010
 -----
 10111


2)
10
3

Returns: 5

The third solution is 5. The first two solutions are 1 and 4.


3)
1
1000000000

Returns: 2000000000



*/
// END CUT HERE
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <bitset>
using namespace std;

class BitwiseEquations {
public:
    long long kthPlusOrSolution(int x, int k) {
		//;
		bitset<64> bx(x);
		bitset<64> bk(k);

		string s(64,'0');

		for(int i=0;i<64;i++)
		{
			if(bk.test(i))
				s[i]='1';
		}

		for(int i=0;i<64;i++)
		{
			if(bx.test(i))
			{
				s.insert(i,"0");
			}
		}

		s=s.substr(0, 64);

		reverse(s.begin(), s.end());
		
		bitset<32> bsh(s.substr(0,32));
		bitset<32> bsl(s.substr(32,32));

		unsigned long long res=bsh.to_ulong();
		res<<=32;

		res|=bsl.to_ulong();

        return (long long)res;
    }

};

// BEGIN CUT HERE
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))

template<typename T> void print( T a ) {
    cerr << a;
}
static void print( long long a ) {
    cerr << a << "L";
}
static void print( string a ) {
    cerr << '"' << a << '"';
}
template<typename T> void print( vector<T> a ) {
    cerr << "{";
    for ( int i = 0 ; i != a.size() ; i++ ) {
        if ( i != 0 ) cerr << ", ";
        print( a[i] );
    }
    cerr << "}" << endl;
}
template<typename T> void eq( int n, T have, T need ) {
    if ( have == need ) {
        cerr << "Case " << n << " passed." << endl;
    } else {
        cerr << "Case " << n << " failed: expected ";
        print( need );
        cerr << " received ";
        print( have );
        cerr << "." << endl;
    }
}
template<typename T> void eq( int n, vector<T> have, vector<T> need ) {
    if( have.size() != need.size() ) {
        cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements.";
        print( have );
        print( need );
        return;
    }
    for( int i= 0; i < have.size(); i++ ) {
        if( have[i] != need[i] ) {
            cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << ".";
            print( have );
            print( need );
            return;
        }
    }
    cerr << "Case " << n << " passed." << endl;
}
static void eq( int n, string have, string need ) {
    if ( have == need ) {
        cerr << "Case " << n << " passed." << endl;
    } else {
        cerr << "Case " << n << " failed: expected ";
        print( need );
        cerr << " received ";
        print( have );
        cerr << "." << endl;
    }
}
// END CUT HERE


// BEGIN CUT HERE
void main( int argc, char* argv[] ) {
    {
        BitwiseEquations theObject;
        eq<long long>(0, theObject.kthPlusOrSolution(5, 1),2L);
    }
    {
        BitwiseEquations theObject;
        eq<long long>(1, theObject.kthPlusOrSolution(5, 5),18L);
    }
    {
        BitwiseEquations theObject;
        eq<long long>(2, theObject.kthPlusOrSolution(10, 3),5L);
    }
    {
        BitwiseEquations theObject;
        eq<long long>(3, theObject.kthPlusOrSolution(1, 1000000000),2000000000L);
    }
}
// END CUT HERE

